321 - The New Villa (Grafos, BFS, bitwise)
[andmenj-acm.git] / 850 - Crypt kicker II / 850.cpp
blobafdabbadd1fdfb6f5a745018d23dab386badff49
1 #include <vector>
2 #include <map>
3 #include <iostream>
4 #include <string>
6 using namespace std;
8 map<char, char> solution;
10 const string key = "the quick brown fox jumps over the lazy dog";
12 bool solve(const string &s){
13 map<char, char> m, n;
14 if (s.length() != key.length()){
15 return false;
17 for (int i=0; i<s.length(); ++i){
18 if (s[i] == ' '){
19 if (key[i] != ' ') return false;
20 }else{
21 //mapear s[i] a key[i]
22 if (m.count(s[i]) > 0){ //ya estaba mapeada
23 if (m[s[i]] != key[i]){
24 return false;
26 }else{
27 if (n.count(key[i]) > 0){
28 if (n[key[i]] != s[i]) return false;
30 m[s[i]] = key[i];
31 n[key[i]] = s[i];
35 solution = m;
36 return true;
39 int main(){
40 int cases;
41 cin >> cases;
42 getchar();
43 getchar();
44 for (int k = 0; k < cases; ++k){
45 if (k>0) cout << endl;
47 vector<string> lines;
48 string l;
49 bool solved = false;
50 // cout << "Caso " << cases << endl;
51 while (getline(cin, l) && l != ""){
52 lines.push_back(l);
53 //cout << l << " - Solution: " << (solve(l)?"True":"False") << endl;
54 if (!solved) solved = solve(l);
57 if (solved){
58 for (int i=0; i<lines.size(); ++i){
59 string s = lines[i];
60 for (int j=0; j<s.size(); ++j){
61 if (s[j] == ' ') cout << " ";
62 else{
63 cout << solution[s[j]];
66 cout << endl;
68 }else{
69 cout << "No solution.\n";
73 return 0;